﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SGCG.Model
{
    public class Rectangle : CompositeBorderedGraphicObject
    {
        private Line _left, _top, _right, _bottom;

        public Rectangle(GraphicObject parent) : base(parent) { }

        protected override IEnumerable<GraphicObject>  BuildObject()
        {
            _left = new Line(this);
            _top = new Line(this);
            _right = new Line(this);
            _bottom = new Line(this);

            yield return _left;
            yield return _top;
            yield return _right;
            yield return _bottom;
        }

        public int X1
        {
            get { return this._left.X1; }

            set
            {
                _left.X1 = value;
                _left.X2 = value;
                _top.X1 = value;
                _bottom.X1 = value;
            }
        }

        public int X2
        {
            get { return this._right.X1; }

            set
            {
                _right.X1 = value;
                _right.X2 = value;
                _top.X2 = value;
                _bottom.X2 = value;
            }
        }

        public int Y1
        {
            get { return this._top.Y2; }

            set
            {
                _top.Y1 = value;
                _top.Y2 = value;
                _left.Y1 = value;
                _right.Y1 = value;
            }
        }

        public int Y2
        {
            get { return this._bottom.Y2; }

            set
            {
                _left.Y2 = value;
                _right.Y2 = value;
                _bottom.Y1 = value;
                _bottom.Y2 = value;
            }
        }

        protected void CopyProperties(Rectangle other)
        {
            base.CopyProperties(other);
            other.X1 = X1;
            other.Y1 = Y1;
            other.X2 = X2;
            other.Y2 = Y2;
        }

        public override GraphicObject CreateCopy()
        {
            Rectangle rectangle = new Rectangle(null);
            CopyProperties(rectangle);
            return rectangle;
        }

        public int ComputedX1 { get; private set; }
        public int ComputedY1 { get; private set; }
        public int ComputedX2 { get; private set; }
        public int ComputedY2 { get; private set; }

        private void Recompute()
        {
            Point p1 = TransformPoint(new Point(X1, Y1));
            ComputedX1 = (int)Math.Round(p1.X);
            ComputedY1 = (int)Math.Round(p1.Y);
            Point p2 = TransformPoint(new Point(X2, Y2));
            ComputedX2 = (int)Math.Round(p2.X);
            ComputedY2 = (int)Math.Round(p2.Y);
        }

        protected override Point GetBarycenter()
        {
            Recompute();
            return new Point((double)(ComputedX1 + ComputedX2) / 2, (double)(ComputedY1 + ComputedY2) / 2);
        }
    }
}
